package com.nineclock.system.controller;

import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.system.OrganizationControllerApi;
import com.nineclock.system.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.service.SysCompanyUserService;
import com.nineclock.system.service.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/organization")
public class OrganizationController implements OrganizationControllerApi {


    @Autowired
    SysDepartmentService departmentService;

    @Autowired
    SysCompanyUserService companyUserService;

    @Override
    @GetMapping("/members/simple")
    public Result<List<SysCompanyUserDTO>> queryCompanyMemberList() {
        List<SysCompanyUserDTO> companyUserDTOList = companyUserService.queryCompanyMemberList();
        return Result.success(companyUserDTOList);
    }

    @Override
    @GetMapping("/department")
    public Result<List<SysDepartmentDTO>> queryDepartmentList() {
        List<SysDepartmentDTO> departmentDTOList = departmentService.queryDepartmentList();
        return Result.success(departmentDTOList);
    }

    @Override
    @GetMapping("/members")
    public Result<PageResult<SysCompanyUserDTO>> members(
            @RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize",required = false, defaultValue = "10") Integer pageSize,
            String keyword,
            @RequestParam(value = "departmentId", required = false, defaultValue = "0") Long departmentId) {

        PageResult<SysCompanyUserDTO>  pageResult = companyUserService.queryMembers(page, pageSize, keyword, departmentId);
        return Result.success(pageResult);
    }

    @Override
    @PostMapping("/uploadExcel")
    public Result uploadExcel(MultipartFile excelFile) throws IOException {
        companyUserService.importData(excelFile);
        return Result.success();
    }

    @Override
    @GetMapping("/department/top")
    public Result<List<Long>> queryDepartmentIds() {
        List<Long> departmentIds = departmentService.queryDepartmentIds();
        return Result.success(departmentIds);
    }

    @Override
    @GetMapping("/department/membernum")
    public Result<Integer> queryUserCountByDepartmentId(@RequestParam("departmentIds") List<Long> departmentIds) {
        Integer count = departmentService.queryUserCountByDepartmentId(departmentIds);
        return Result.success(count);
    }

    @Override
    @GetMapping("/simple")
    public Result<List<DepartmentOrUserSimpleDTO>> querySimpleDepartment(
            @RequestParam(value = "id", required = false) Long departmentId,
            @RequestParam(value = "includeMember", required = false) Integer includeMember) {
        List<DepartmentOrUserSimpleDTO> userSimpleDTOList = departmentService.querySimpleDepartment(departmentId, includeMember);
        return Result.success(userSimpleDTOList);
    }
}
